// src/composables/useImageUpload.ts
import { ref } from 'vue'
import * as Api from "@/api/index";
import { isCompleteURL } from "@/utils/util";

export function useImageUpload(serverUrl: string) {
  const uploadFile = async (file: File, apiEndpoint: string) => {
    try {
      const formData = new FormData()
      formData.append('file', file)
      
      const response = await Api.upload(apiEndpoint, formData, '', {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      
      return isCompleteURL(response.url) ? response.url : `${serverUrl}${response.url}`
    } catch (error) {
      console.error('上传失败:', error)
      throw error
    }
  }

  return {
    uploadFile
  }
}